import axios, { AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from 'axios';
import { layer } from "@layui/layer-vue";
import Qs from 'qs'
type TAxiosOption = {
    timeout: number;
    baseURL: string;
}

const config: TAxiosOption = {
    timeout: 50000,
    baseURL: ""
}
let layerid = "";

class Http {
    service;
    constructor(config: TAxiosOption) {
        this.service = axios.create(config)

        /* 请求拦截 */
        this.service.interceptors.request.use((config: AxiosRequestConfig) => {
            // const userInfoStore = useUserStore();
            let access_token = "";
            try {
                access_token = JSON.parse(localStorage.user).token
            } catch (ex) {

            }

            let win = window as any;
            if (access_token != "") {
                (config.headers as AxiosRequestHeaders).Authorization = "bearer " + access_token as string

            } else {


            }
            return config
        }, error => {
            return Promise.reject(error);
        })

        /* 响应拦截 */
        this.service.interceptors.response.use((response: AxiosResponse<any>) => {
            if (layerid!="") {
                layer.close(layerid);
                layerid = "";
            }
            let win = window as any;
            switch (response.status) {
                case 200:
                    return response.data;
                case 500:
                    return response.data;
                case 401:
                    win.location.href = '/login';
                    // layer.confirm(
                    // '会话超时, 请重新登录', 
                    // { icon : 2, yes: function(){
                    //     router.push('/login');
                    //     layer.closeAll()
                    // }});
                    return response;
                default:
                    break;
            }
        }, error => {
            if (layerid!="") {
                layer.close(layerid);
                layerid = "";
            }
            let win = window as any;
            if (error.response.status == 401) {
                win.location.href = '/login';
                // layer.confirm(
                //     '会话超时, 请重新登录', 
                //     { icon : 2, yes: function(){
                //         router.push('/login');
                //         layer.closeAll()
                //     }});
            } else {
                return Promise.reject(error)
            }


        })
    }

    /**
     * get请求
     * @param url 请求地址可在localStorage.httpversion配置
     * @param params 参数
     * @param _object 
     * @returns 
     */
    get<T>(url: string, params?: object, _object = {}): Promise<any> {
        let win = window as any;
        let host = ""

        if (url.indexOf("http") > -1) {
            host = url;
        } else {
            if (win.localStorage.httpversion != null) {
                host = win.localStorage.httpversion + url;
            }
        }
        return this.service.get(host, { params, ..._object })
    }
    /**
     * post
     * @param url 
     * @param params 
     * @param _object 
     * @returns 
     */
    post<T>(url: string, params?: object, msg?, _object = {}): Promise<any> {
        if (msg) {
            layerid = layer.msg(msg, { icon: 16, shade: true });
        }

        let win = window as any;
        let host = ""
        if (url.indexOf("http") > -1) {
            host = url;
        } else {
            if (win.localStorage.httpversion != null) {
                host = win.localStorage.httpversion + url;
            }
        }
        return this.service.post(host, Qs.stringify(params))
    }
    /**
     * 上传文件
     * @param url 
     * @param params 
     * @param _object { headers: { 'Content-Type': 'multipart/form-data' }
     * @returns 
     */
    uploadpost<T>(url: string, params?: FormData, msg?, _object = {}): Promise<any> {
        if (msg) {
            layerid = layer.msg(msg, { icon: 16, shade: true });
        }
        let win = window as any;
        let host = ""
        if (url.indexOf("http") > -1) {
            host = url;
        } else {
            if (win.localStorage.httpversion != null) {
                host = win.localStorage.httpversion + url;
            }
        }

        return this.service.post(host, params, _object)
    }
    //     axios.post(url.value, f, { headers: { 'Content-Type': 'multipart/form-data' } }).then(res => {
    //    console.log(res);
    //     if (res.data) {

    //     }
    // }).catch(res => {

    // });
    /* PUT 方法 */
    put<T>(url: string, params?: object, msg?, _object = {}): Promise<any> {
        if (msg) {
            layerid = layer.msg(msg, { icon: 16, shade: true });
        }
        let win = window as any;
        let host = ""
        if (url.indexOf("http") > -1) {
            host = url;
        } else {
            if (win.localStorage.httpversion != null) {
                host = win.localStorage.httpversion + url;
            }
        }
        return this.service.put(host, params, _object)
    }
    /* DELETE 方法 */
    delete<T>(url: string, params?: any, msg?, _object = {}): Promise<any> {
        if (msg) {
            layerid = layer.msg(msg, { icon: 16, shade: true });
        }
        let win = window as any;
        let host = ""
        if (url.indexOf("http") > -1) {
            host = url;
        } else {
            if (win.localStorage.httpversion != null) {
                host = win.localStorage.httpversion + url;
            }
        }
        return this.service.delete(host, { params, ..._object })
    }
}

export default new Http(config)